package com.lemner.www.hongyu.Activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lemner.www.hongyu.Bean.Userbean;
import com.lemner.www.hongyu.R;

import com.lemner.www.hongyu.Service.Service;
import com.lemner.www.hongyu.Static.Prefence;
import com.lemner.www.hongyu.Utils.PrefUtil;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_phonenumber, et_password;
    private TextView tv_forgetpwd;
    private Context mContext = LoginActivity.this;
    private Button btn_login;
    private  Userbean.DataBean date;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Service service;
    private Userbean.DataBean date;
    private static String BASEURL = "http://www.hongyudasha.com/propertysystem/";
    private String fPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        InitView();
        getCatch();
    }

    public void InitView() {
        et_phonenumber = (EditText) findViewById(R.id.et_phonenumber);
        et_password = (EditText) findViewById(R.id.et_password);
        tv_forgetpwd = (TextView) findViewById(R.id.tv_forgetpwd);
        btn_login = (Button) findViewById(R.id.btn_login);
        tv_forgetpwd.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    private void getCatch(){
        String count = PrefUtil.getString(mContext, Prefence.LOAD_COUNT, null);
        String pwd = PrefUtil.getString(mContext, Prefence.LOAD_PWD, null);

        if (!TextUtils.isEmpty(count)){
            et_phonenumber.setText(count);
        }

        if (!TextUtils.isEmpty(pwd)){
            et_password.setText(pwd);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_forgetpwd:
                Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_login:
                if (TextUtils.isEmpty(et_phonenumber.getText())) {
                    Toast.makeText(getApplicationContext(), "电话号为空，请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    getDate();
                    break;
                }
            default:
                break;
        }
    }

    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
<<<<<<< HEAD
    public void getDate(){
        String number=et_phonenumber.getText().toString();
        String pwd=et_password.getText().toString();
       Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         service=retrofit.create(Service.class);
        Call<Userbean>call=service.getLogin(number, md5(pwd));
        call.enqueue( new Callback<Userbean>() {
            @Override
            public void onResponse(Call<Userbean> call, Response<Userbean> response) {
                    date = response.body().getData();
                if(response.body().getRetCode().equals("0001")) {
                    Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("date", date);
                    startActivity(intent);
                    finish();
                }
                else
                    Toast.makeText(getApplicationContext(), response.body().getErrMsg(), Toast.LENGTH_SHORT).show();
            }
                          @Override
                          public void onFailure(Call<Userbean> call, Throwable t) {
                              Log.e("error", t.getMessage());
                          }
                      });


    public void getDate() {
        final String number = et_phonenumber.getText().toString();
        final String pwd = et_password.getText().toString();
        fPwd = md5(pwd);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        LoginService service = retrofit.create(LoginService.class);
        Call<Userbean> call = service.getLogin(number, fPwd);

        call.enqueue(new Callback<Userbean>() {
            @Override
            public void onResponse(Call<Userbean> call, Response<Userbean> response) {
                date = response.body().getData();
                Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("date", date);


                PrefUtil.setString(mContext, Prefence.LOAD_COUNT, number);
                PrefUtil.setString(mContext, Prefence.LOAD_PWD, pwd);

                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(Call<Userbean> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });

    }

}
